/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/
package com.zog
{
	import com.pblabs.engine.PBE;
    import com.pblabs.engine.core.ObjectType;
    import com.pblabs.engine.core.ObjectTypeManager;
    import com.pblabs.engine.debug.Logger;
    import com.pblabs.engine.debug.Profiler;
    import com.pblabs.engine.entity.EntityComponent;
        import com.pblabs.rendering2D.SpriteSheetRenderer;
    import com.pblabs.rendering2D.spritesheet.SpriteSheetComponent;
    import com.pblabs.rendering2D.SimpleSpatialComponent;
    import com.pblabs.rendering2D.SpriteRenderer;
    import com.pblabs.engine.entity.*;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import com.pblabs.box2D.Box2DSpatialComponent;
    import com.pblabs.box2D.Box2DManagerComponent;
    import com.pblabs.box2D.CircleCollisionShape;
    import com.pblabs.box2D.CollisionEvent;
    import com.pblabs.components.basic.HealthComponent;
    
    public class DamageEntityOnCollision extends EntityComponent
    {
        //todo damage self var!
        
        public function get playerCollisionGroup():String
        {
            return _playerCollisionGroup;
        }
        
        public function set playerCollisionGroup(value:String):void
        {
            _playerCollisionGroup = value;
        }
        
        public function get damage():Number
        {
            return _damage;
        }
        
        public function set damage(value:Number):void
        {
            _damage = value;
        }

        private var _playerCollisionGroup:String = "";
        private var _damage:Number = 1.0;
        
        protected override function onAdd():void
        {
            super.onAdd();
      
            owner.eventDispatcher.addEventListener(CollisionEvent.COLLISION_EVENT, onCollision);		
        }
        
    	
		protected override function onRemove():void
		{
			super.onRemove();
			
			owner.eventDispatcher.removeEventListener(CollisionEvent.COLLISION_EVENT, onCollision);
		}
		
		private function onCollision(event:CollisionEvent):void
		{
			for each(var collider:Box2DSpatialComponent in [event.collidee, event.collider])
			{
                if(PBE.objectTypeManager.doesTypeOverlap(collider.collisionType, _playerCollisionGroup))
                {
                    var health:HealthComponent = collider.owner.lookupComponentByType(HealthComponent) as HealthComponent;
                    if(health != null)
                    {
                        health.damage(_damage);
                        //Logger.print(this, "Damage:" + _damage + "\tRemaining Health:" + health.health);
                    }
                    else
                    {
                        Logger.error(this, "OnCollision", "Entity " + collider.owner.name + " has no HealthComponent.");
                    }
                }
			}            
		}
    }
}